package com.timoon.manager.panel.passage;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.wicket.jquery.ui.form.button.AjaxButton;
import com.timoon.app.function.UpdateFunction;
import com.timoon.component.panel.bootstrap.modal.ModalPanel;
import com.timoon.manager.auth.BaseAuthPanel;
import com.timoon.manager.panel.passage.cell.PassageDetailPanel;
import com.timoon.sample.dao.model.InfoSort;
import com.timoon.sample.dao.model.Passage;
import com.timoon.sample.utils.SystemConfig;

/**
 *<p>功能:</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:sky</p>
 *@author 付乐天
 *@version 1.0 */

public class PassageCategoryPanel extends BaseAuthPanel implements UpdateFunction{
	
	@SpringBean(name="manager.passage")
	private PassageManagerService service;
	
	private WebMarkupContainer gridContainer;
	
	private Integer defaultInfoSortId;
	
	private ModalWindow win;
	
	private ModalPanel delWarn;
	
	private List<String> checkboxPool=new ArrayList<String>();

	public PassageCategoryPanel(String id) {
		super(id);
		// TODO Auto-generated constructor stub
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		renderDelWarn();
		
		renderForm();
		
		renderWindow();
	}

	private void renderDelWarn() {
		// TODO Auto-generated method stub
		delWarn=new ModalPanel("wk_del_warn","警告","确认删除","您真的要删除所选吗？") {
			@Override
			public boolean onAction(AjaxRequestTarget target) {
				// TODO Auto-generated method stub
				service.delPassageByIds(checkboxPool);
				doUpdate(target,null);
				return super.onAction(target);
			}
		};
		add(delWarn);
	}

	private void renderWindow() {
		// TODO Auto-generated method stub
		win=new ModalWindow("wk_win");
		add(win);
	}

	private void renderForm() {
		// TODO Auto-generated method stub
		Form form=new Form("wk_form");
		add(form);
		
		renderNewsCategorySelect(form);
		
		renderGridContainer(form);
		
		renderBtnAdd(form);
		
		renderBtnDel(form);
		
	}

	private void renderBtnDel(Form form) {
		// TODO Auto-generated method stub
		form.add(new AjaxButton("wk_del_passage") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				delWarn.showModal(target);
			}
		});
	}

	private void renderBtnAdd(Form form) {
		// TODO Auto-generated method stub
		form.add(new AjaxButton("wk_add_passage") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				
				win.setTitle("修改");
				win.setInitialWidth(800);
				win.setContent(new PassageDetailPanel(win.getContentId(), new Passage(){{
					this.setType(defaultInfoSortId);
				}},win,PassageCategoryPanel.this));
				win.show(target);
				
			}
		});
	}

	private void renderGridContainer(Form form) {
		// TODO Auto-generated method stub
		gridContainer=new WebMarkupContainer("wk_grid_container");
		gridContainer.setOutputMarkupId(true);
		form.add(gridContainer);
		
		renderGrid(null);
	}

	private void renderGrid(AjaxRequestTarget target) {
		// TODO Auto-generated method stub
		
		List<Passage> passages=service.getPassagesByType(defaultInfoSortId);
		
		PageableListView<Passage> listView=new PageableListView<Passage>("wk_list",passages,20) {
			
			@Override
			protected void populateItem(ListItem<Passage> arg0) {
				// TODO Auto-generated method stub
				final Passage passage=arg0.getModelObject();
				
				arg0.add(new Label("wk_id",passage.getId()));
				arg0.add(new Label("wk_title",passage.getTitle()));
				arg0.add(new Label("wk_date", passage.getUpdateDate()));
				arg0.add(new Label("wk_author", passage.getAuthor()));
				arg0.add(new Label("wk_read", passage.getReadCount()));
				arg0.add(new Label("wk_publiced", passage.getPubliced()));
				
				arg0.add(new AjaxCheckBox("wk_checkbox",new Model<Boolean>(false)) {
					
					@Override
					protected void onUpdate(AjaxRequestTarget arg0) {
						// TODO Auto-generated method stub
						if((boolean) this.getDefaultModelObject()){
							checkboxPool.add(passage.getId().toString());
						}else{
							checkboxPool.remove(passage.getId().toString());
						}
					}
				});
				
				arg0.add(new AjaxLink("wk_modify") {

					@Override
					public void onClick(AjaxRequestTarget target) {
						// TODO Auto-generated method stub
						win.setTitle("修改");
						win.setInitialWidth(800);
						win.setContent(new PassageDetailPanel(win.getContentId(), passage,win,PassageCategoryPanel.this));
						win.show(target);
					}
				});
			}
		};
		
		gridContainer.addOrReplace(listView);
		gridContainer.addOrReplace(new AjaxPagingNavigator("wk_page",listView));
		
		if(target!=null){
			target.add(gridContainer);
		}
	}

	private void renderNewsCategorySelect(Form form) {
		// TODO Auto-generated method stub
		
		List<InfoSort> infoSorts=this.service.getInfoSortsByType(SystemConfig.INFO_SORT_TYPE_NEWS);
		
		if(infoSorts.size()>0){
			defaultInfoSortId=infoSorts.get(0).getId();
		}else{
			defaultInfoSortId=-1;
		}
		
		IChoiceRenderer<InfoSort> render=new ChoiceRenderer<InfoSort>(){
			@Override
			public Object getDisplayValue(InfoSort object) {
				// TODO Auto-generated method stub
				return object.getName();
			}
			@Override
			public String getIdValue(InfoSort object, int index) {
				// TODO Auto-generated method stub
				if(object.getId()==null){
					return super.getIdValue(object, index);
				}else{
					return object.getId().toString();
				}
			}
		};
		
		
		
		final DropDownChoice<InfoSort> choice=new DropDownChoice<InfoSort>("wk_news_category",new Model<InfoSort>(new InfoSort()), infoSorts,render);
		
		choice.add(new AjaxFormComponentUpdatingBehavior("onChange") {
			
			@Override
			protected void onUpdate(AjaxRequestTarget arg0) {
				// TODO Auto-generated method stub
				InfoSort info=(InfoSort) choice.getDefaultModelObject();
				defaultInfoSortId=info.getId();
				renderGrid(arg0);
			}
		});
		
		form.add(choice);
	}

	@Override
	public void doUpdate(AjaxRequestTarget target, Object obj) {
		// TODO Auto-generated method stub
		this.renderGrid(target);
	}

}
